import React, { useEffect, useState } from "react";
import gameApi, { Game } from "../api/game-api";

const useGames=() => {
    const [games, setGames] = useState<Game[]>([]);
    const [error, setError] = useState("");
  
    // 加载数据
    useEffect(() => {
      const { request, cancel } = gameApi.getGames();
      request
        .then((res) => {
          const { data, status } = res;
          if (status === 200) {
            setGames(data.results);
          }
        })
        .catch((err) => {
          setError(err.message);
        });
    }, []);

    return {games,error}
}

export default useGames